package cn.dnui_wwt524.dnuifood_16110100524.controller;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;
import java.util.List;
import cn.dnui_wwt524.dnuifood_16110100524.Adapter.FoodListAdapter;
import cn.dnui_wwt524.dnuifood_16110100524.R;
import cn.dnui_wwt524.dnuifood_16110100524.bean.CollectStatus;
import cn.dnui_wwt524.dnuifood_16110100524.bean.FoodBean;
import cn.dnui_wwt524.dnuifood_16110100524.listener.OnRetrofitListener;
import cn.dnui_wwt524.dnuifood_16110100524.model.FoodModel;
import cn.dnui_wwt524.dnuifood_16110100524.service.FoodService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodListActivity extends AppCompatActivity implements OnRetrofitListener<List<FoodBean>> {
    private FoodListAdapter foodListAdapter;
    private RecyclerView recyclerView;
    String shop_id;
    private Menu menu;
    private String result;
    private Context context;
    String userid;

    void initViews() {
        recyclerView = (RecyclerView)findViewById(R.id.food_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_foodlist);
        SharedPreferences sharedPreferences = getSharedPreferences("userinfo", MODE_PRIVATE);
        userid = (String)sharedPreferences.getString("userid", "");
        shop_id = getIntent().getStringExtra("shop_id");

        initViews();
        initData();

    }
    private void initData() {
        FoodModel foodModel = new FoodModel();
        foodModel.getFoodByShops(shop_id,this);//访问服务器请求
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        //创建菜单
        getMenuInflater().inflate(R.menu.menu_collect,menu);
        this.menu=menu;
        //菜单创建完毕后向服务器发送请求，判断是否已经收藏
        iscollected();
        return super.onCreateOptionsMenu(menu);
    }

    private void iscollected(){
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://172.24.10.175:8080/foodService/").addConverterFactory(GsonConverterFactory.create()).build();
        FoodService foodService = retrofit.create(FoodService.class);

        Call<CollectStatus> call = foodService.isCollected(userid,shop_id,"0");
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                // listener.onSuccess(response.body(),5);
                if (response.body().getCollected().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("取消收藏");
                }else {
                    menu.findItem(R.id.item_collect).setTitle("收藏");
                }
            }
            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {
                Toast.makeText(FoodListActivity.this,"网络连接失败",Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        //实现"显示收藏还是取消收藏"
        switch (item.getItemId()){
            case R.id.item_collect:
                String title = item.getTitle().toString();
                if (title.equals("收藏"))
                {
                    //收藏店铺
                    collectShop();
                }
                else
                {
                    //取消收藏
                    uncollectShop();
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void collectShop() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://172.24.10.175:8080/foodService/").addConverterFactory(GsonConverterFactory.create()).build();
        FoodService foodService = retrofit.create(FoodService.class);

        Call<CollectStatus> call = foodService.userCollectShop(userid,shop_id);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("取消收藏");
                    Toast.makeText(FoodListActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(FoodListActivity.this, "收藏店铺失败", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {
                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);
    }

    private void uncollectShop() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://172.24.10.175:8080/foodService/").addConverterFactory(GsonConverterFactory.create()).build();
        FoodService foodService = retrofit.create(FoodService.class);

        Call<CollectStatus> call = foodService.userCollectShop(userid,shop_id);
        Callback<CollectStatus> callback = new Callback<CollectStatus>() {
            @Override
            public void onResponse(Call<CollectStatus> call, Response<CollectStatus> response) {
                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("收藏");
                    Toast.makeText(FoodListActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();

                }else {
                    Toast.makeText(FoodListActivity.this, "取消收藏失败", Toast.LENGTH_SHORT).show();
                }

            }
            @Override
            public void onFailure(Call<CollectStatus> call, Throwable t) {
                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);
    }

    @Override
    public void onSuccess(List<FoodBean> object, int flag) {
        switch (flag) {
            case Constants.GETFOODBYSHOP:
                foodListAdapter = new FoodListAdapter(FoodListActivity.this, (List<FoodBean>) object, R.layout.activity_foodlist);
                recyclerView.setAdapter(foodListAdapter);
                break;
    }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(this, "网络连接失败", Toast.LENGTH_SHORT).show();

    }
    public class Constants{
        public final static String BASE_URL="http://172.24.10.175:8080/foodService/";
        public final static int GETFOODBYSHOP=1;
        public final static int ISCOLLECTED=2;
        public final static int USERCOLLECTFOOD=3;
    }
}
